package com.googlecode.jascii.printing;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;



public class FoundPrinter {

	public static PrintService getNamedPrintService(String prnName)
	  throws PrinterNotFoundException
	  {
		  if(prnName==null) return PrintServiceLookup.lookupDefaultPrintService();
	      PrintService[] prnSvcs;
	      PrintService prnSvc = null;
	      // get all print services for this machine
	      prnSvcs = PrintServiceLookup.lookupPrintServices(null, null);
	    
	      if (prnSvcs.length > 0)
	      {
	          int ii = 0;
	          while ( ii < prnSvcs.length )
	          {
	              System.out.println("Named Printer found: "+prnSvcs[ii].getName());
	              if (prnSvcs[ii].getName().equalsIgnoreCase(prnName))
	              {
	                  prnSvc = prnSvcs[ii];
	                  System.out.println("Named Printer selected: "+prnSvcs[ii].getName()+"*");
	                  break;
	              }
	              ii++;
	          }
	      }
	    
	      if (prnSvc == null)
	      {
	          throw new PrinterNotFoundException("Printer " + prnName + " was not found on this system.");
	      }
	    
	      return prnSvc;
	  }
	
	public static String[] getPrintServiceName()
	{
		PrintService[] prnSvcs  = PrintServiceLookup.lookupPrintServices(null, null);
		String[] printNms = new String[prnSvcs.length];
		if (prnSvcs.length > 0)
	      {
	          int ii = 0;
	          while ( ii < prnSvcs.length )
	          {
	              System.out.println("Named Printer found: "+prnSvcs[ii].getName());
	              printNms[ii] = prnSvcs[ii].getName();
	              ii++;
	          }
	      }
	    return printNms;
	}
	
	
}
